package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.model.SeckillGoods;
import com.atguigu.gmall.actvitiy.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-09-23 09:18
 */
@Controller
public class SeckillHtmlController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    /**
     * 渲染秒杀列表页面
     *
     * @param model
     * @return
     */
    @GetMapping("/seckill.html")
    public String seckillListHtml(Model model) {
        Result<List<SeckillGoods>> result = activityFeignClient.getSeckillGoods();
        model.addAttribute("list", result.getData());
        return "/seckill/index";
    }

    @GetMapping("/seckill/{skuId}.html")
    public String seckillItemHtml(Model model, @PathVariable("skuId") Long skuId) {
        Result<SeckillGoods> result = activityFeignClient.getSecGoodsById(skuId);
        model.addAttribute("item", result.getData());
        return "/seckill/item";
    }

    /**
     * 秒杀排队页面
     *
     * @param model
     * @param skuId
     * @param skuIdStr
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String seckillQueueHtml(Model model, @RequestParam("skuId") Long skuId, @RequestParam("skuIdStr") String skuIdStr) {
        model.addAttribute("skuId", skuId);
        model.addAttribute("skuIdStr", skuIdStr);
        return "/seckill/queue";
    }

    /**
     * 秒杀订单确认页面渲染
     * @param model
     * @param skuId
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String seckillTradeHtml(Model model, @RequestParam("skuId") Long skuId){
        Result<Map> result = activityFeignClient.seckillTradeData(skuId);
        model.addAllAttributes(result.getData());
        return "/seckill/trade";
    }
}
